﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace DecisionCircle.Platform
{
    public partial class Circle
    {
        #region Variable
        private double dtotalRotationRadius;
        private int intLevel;
        private int intNumOfColors;
        private int intNumOfEdges;
        private int intRadius;
        private double dRadius;
        private Point pLeftTop;
        private List<Polygon> listEdges;
        private Path pathCircle;
        private Point pointCenter;
        #endregion

        #region Constructor
        public Circle()
        {
 
        }

        public Circle(int intLevel, int intNumOfColors)
        {
            InitializeFeature(intLevel, intNumOfColors, Constant.IntRadiusDefault, Constant.PointLeftTopDefault);
        }

        public Circle(int intLevel, int intNumOfColors, Point pLeftTop)
        {
            InitializeFeature(intLevel, intNumOfColors, Constant.IntRadiusDefault,pLeftTop);
        }

        public Circle(int intLevel, int intNumOfColors, int intRadius, Point pLeftTop)
        {
            InitializeFeature(intLevel, intNumOfColors, intRadius, pLeftTop);
        }
        #endregion

        #region Initialize
        private void InitializeFeature(int intLevel, int intNumOfColors, int intRadius, Point pLeftTop)
        {
            this.intNumOfColors = intNumOfColors;
            this.intRadius = intRadius;
            this.pLeftTop = pLeftTop;
            this.intLevel = intLevel;

            InitializeListEdges();
            InitializeCalculation();

            this.dtotalRotationRadius = dRadius / 2;
        }

        private void InitializeCalculation()
        {
            SetNumOfEdges();
            SetRadius();
            CreateListTriangle();
        }

        private void InitializeListEdges()
        {
            listEdges = new List<Polygon>();
        }
        #endregion

        #region Properties
        public Path PathCircle
        {
            get { return pathCircle; }
            private set { }
        }

        public Point PointCenter
        {
            get { return pointCenter; }
            private set { }
        }
        #endregion

        #region Public Method
        public void SetParent(ref Grid gridParent)
        {
            foreach (var triangle in listEdges)
            {
                gridParent.Children.Add(triangle);
            }
        }

        public void Rotate(int vec)
        {
            dtotalRotationRadius += vec * Math.PI/180;
            dtotalRotationRadius = Tool.DoubleMod(dtotalRotationRadius, Math.PI * 2);
            foreach (var triangle in listEdges)
            {
                (triangle.RenderTransform as CompositeTransform).Rotation += vec;
            }
        }

        public int GetIndexOfTheChosenOne()
        {
            try
            {
                double alpha = Constant.GetTargetRadian(pointCenter);

                dtotalRotationRadius = Tool.DoubleMod(dtotalRotationRadius, 2 * Math.PI);

                for (int i = 0; i < intNumOfEdges; i++)
                {
                    double temp = dtotalRotationRadius + i * dRadius;
                    if (temp < 0) temp += 2 * Math.PI;
                    temp = Tool.DoubleMod(temp, Math.PI * 2) - alpha;
                    if (temp <= dRadius && temp >= 0)
                    {
                        return i % intNumOfColors;
                    }
                }

                return -1;
            }
            catch { return -1; }
        }
        #endregion

        #region Private Function
        private void SetNumOfEdges()
        {
            intNumOfEdges = intNumOfColors * intLevel;
 
            while (intNumOfEdges < Constant.IntMinimumEdges)
            {
                intNumOfEdges += intNumOfColors;
            }
        }

        private void SetRadius()
        {
            dRadius = (2*Math.PI / (double)intNumOfEdges);
        }

        private void CreateRootTriangle()
        {
            listEdges.Add(Triangle.CreateTriangle(pLeftTop, dRadius, intRadius, Constant.ListColor[0], ref pointCenter));
        }

        private void CreateListTriangle()
        {
            CreateRootTriangle();
            for (int i = 1; i < intNumOfEdges; i++)
            {
                var temp = listEdges[i - 1];
                listEdges.Add(Triangle.CreateTriangle(ref temp, dRadius, Constant.ListColor[i%intNumOfColors]));
            }
        }
        #endregion
    }
}
